package com.service.impl;

import com.ReturnDTO.ReturnDTO;
import com.entity.User;
import com.mapper.UserMapper;
import com.service.RegistrationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.util.ObjectUtils;

/**
 * 注册业务层
 * @author Administrator
 */
@Service
public class RegistrationServiceImpl implements RegistrationService{

  @Autowired
  private UserMapper userMapper;

  /**
   * 注册 获取用户名，判断是否存在已有用户名（500，已存在）
   * @param user
   * @return
   */
  @Override
  public ReturnDTO register(User user) {
    User fromUser = userMapper.selectUserByUserName(user.getUsername());
    if (!ObjectUtils.isEmpty(fromUser)){
      return ReturnDTO.getReturnDTO(500,"用户名已经注册");
    }
    /**
     * MD5加密
     */
    user.setPassword(DigestUtils.md5DigestAsHex(user.getPassword().getBytes()));
    //返回影响行数
    int rows = userMapper.insertUser(user);
    //判断是否影响行数为0
    if (rows == 0){
      return ReturnDTO.getReturnDTO(500,"用户注册失败");
    }
    return ReturnDTO.getReturnDTO(200,"用户注册成功","/toLoginPage");

  }


}
 